#include "stm32f10x.h"
#include "nixie_tube.h"
#include <stdio.h>

void nixie_tube_gpio_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  /* Configure PD0 and PD2 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 | TUBE_SEGMENT_CHOOSE_A4 | 
	                              TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D |
	                               TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G | TUBE_BIT_CHOOSE_H;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 | TUBE_SEGMENT_CHOOSE_A4 );
	
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D |
	                    TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G | TUBE_BIT_CHOOSE_H);

}

void nixie_tube_show_zero(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_G );
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F);
}

void nixie_tube_show_one(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G);
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C);
}

void nixie_tube_show_two(int num)
{
  GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_F);
	
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E |  TUBE_BIT_CHOOSE_G);
}

void nixie_tube_show_three(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F);
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_G);
}

void nixie_tube_show_four(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E);
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C  | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G);
}
void nixie_tube_show_five(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_E);
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G);
}

void nixie_tube_show_six(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_B);
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G);
}
void nixie_tube_show_seven(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G );
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C);
}

void nixie_tube_show_eight(int num)
{
  //GPIO_SetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 );
	GPIO_SetBits(GPIOB, TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G );

	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G);
}
void nixie_tube_show_nine(int num)
{
  GPIO_SetBits(GPIOB,  TUBE_BIT_CHOOSE_D | TUBE_BIT_CHOOSE_E  );
	//GPIO_ResetBits(GPIOB, TUBE_SEGMENT_CHOOSE_A1 | TUBE_SEGMENT_CHOOSE_A2 | TUBE_SEGMENT_CHOOSE_A3 | TUBE_SEGMENT_CHOOSE_A4);
	GPIO_ResetBits(GPIOB, TUBE_BIT_CHOOSE_A | TUBE_BIT_CHOOSE_B | TUBE_BIT_CHOOSE_C | TUBE_BIT_CHOOSE_F | TUBE_BIT_CHOOSE_G);
}

void nixie_tube_show(int num)
{
   switch (num) {
	 case 0:
		 nixie_tube_show_zero(1);
		 break;
	 case 1:
		 nixie_tube_show_one(1);
		 break;
	 case 2:
		 nixie_tube_show_two(1);
		 break;
	 case 3:
		 nixie_tube_show_three(1);
		 break;
	 case 4:
		 nixie_tube_show_four(1);
		 break;
	 case 5:
		 nixie_tube_show_five(1);
		 break;
	 case 6:
		 nixie_tube_show_six(1);
		 break;
	 case 7:
		 nixie_tube_show_seven(1);
		 break;
	 case 8:
		 nixie_tube_show_eight(1);
		 break;
	 case 9:
		 nixie_tube_show_nine(1);
		 break;
	 }




}